package com.platform.common.config.shiro;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Shiro Cache管理器
 * 
 * @author LiangMingHui
 *
 */
public class ShiroCacheManager implements CacheManager {

	private final static String KEY_PREFIX = "SHIRO_CACHE:";

	@SuppressWarnings("rawtypes")
	private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();

	@SuppressWarnings("unchecked")
	@Override
	public <K, V> Cache<K, V> getCache(String name) throws CacheException {
		Cache<K, V> cache = caches.get(name);
		if (null == cache) {
			// 创建缓存
			cache = new ShiroCache<K, V>(KEY_PREFIX);
			// 存入缓存
			caches.put(name, cache);
		}
		return null;
	}
}